using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public static class ReplaceTools
    {
        private static readonly Dictionary<string, string> map;

        static ReplaceTools()
        {
            var settings = LoadSettings();
            map = new Dictionary<string, string>();
            for (var i = 0; i < settings.replaceItems.Length; i++)
                map.Add(settings.replaceItems[i].key, settings.replaceItems[i].value);
        }

        private static ReplaceSettings LoadSettings()
        {
            var loadedSettings = (ReplaceSettings)Resources.Load("ReplaceSettings", typeof(ReplaceSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<ReplaceSettings>();
                Debug.LogError("ReplaceSettings is not exist (Resources)");
            }

            return loadedSettings;
        }

        public static string Get(string key)
        {
            if (map.ContainsKey(key)) return map[key];
            return "";
        }

        public static string GetSprite(string type, string id)
        {
            return Get(string.Format("{0}|{1}", type, id));
        }
    }
}